import Vue from "vue";
import Vuex from "vuex";
import HS from "../assets/js/hs.common";

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    hs: HS, // hs焦点
    bridge: {}, // 桥
    // domain: "125.210.113.200:8080",
    domain: "125.210.160.14:8080",
    navigationShow: true, //导航条是否显示
    appShow: true, // 整个应用是否显示
    appBg: require("../assets/images/bg.jpg"), // 应用背景图
    appBgInfo: {
      // 应用背景图选择
      xyfc: require("../assets/images/xyfc/xyfc_bg.png"),
      xygg: require("../assets/images/xyfc/xygg.png"),
      yckt: require("../assets/images/yckt/bg.png"),
      default: require("../assets/images/bg.jpg")
    },
    vedioInfo: {
      //  视频参数
      content: "1920*1080",
      offsetLeft: 0,
      offsetTop: 0,
      width: 1920,
      height: 1080,
      type: 2, // 历史
      assetType: -1, // 内容类型
      programType: -1, // 节目类型
      contentId: -1 // 内容标识
    },
    live: {
      // 直播
      defaultImg: "",
      status: "未直播"
    },
    school: {}, // 学校
    user: {} //  用户
  },
  getters: {
    school(state) {
      if(Object.keys(state.school).length > 0) return state.school
      let school;
      if (localStorage) {
        school = localStorage.getItem("school");
      } else {
        school = state.bridge.getSharepreValue({
          name: "hzjn",
          key: "school"
        });
      }
      if (school) {
        state.school = JSON.parse(school);
      }
      return state.school;
    },
    user(state) {
      if(Object.keys(state.user).length > 0) return state.user
      let student;
      if (localStorage) {
        student = localStorage.getItem("student");
      } else {
        student = state.bridge.getSharepreValue({
          name: "hzjn",
          key: "student"
        });
      }
      if (student) {
        state.user = JSON.parse(student);
      }
      return state.user;
    }
  },
  mutations: {
    initHs(state) {
      state.hs.objects = {};
      state.hs.areaLastFocus = {};
      state.hs.focObj = {};
      state.hs.focArea = null;
      state.hs.prevFocObj = {};
      state.hs.prevArea = null;
      state.hs.direction = null;
      state.hs.clearList();
    },
    setNavigationShow(state, payload) {
      state.navigationShow = payload;
    },
    setAppShow(state, payload) {
      state.appShow = payload;
    },
    setVideoInfo(state, payload) {
      for (let key in state.vedioInfo) {
        if (payload[key] !== undefined && payload[key] !== null) {
          state.vedioInfo[key] = payload[key];
        }
      }
    },
    setAppBg(state, payload) {
      state.appBg = payload;
    },
    setBridge(state, payload) {
      state.bridge = payload;
    },
    setSchool(state, payload) {
      state.school = payload;
      state.bridge.sharepreSave({
        name: "hzjn",
        key: "school",
        value: JSON.stringify(payload)
      });
      // state.hs.cookie.set("school", JSON.stringify(payload));
      localStorage.setItem("school", JSON.stringify(payload));
    },
    setLive(state, payload) {
      for (let key in state.live) {
        if (payload[key] !== undefined && payload[key] !== null) {
          state.live[key] = payload[key];
        }
      }
    },
    setUser(state, payload) {
      state.user = payload;
      state.bridge.sharepreSave({
        name: "hzjn",
        key: "student",
        value: JSON.stringify(payload)
      });
      localStorage.setItem("student", JSON.stringify(payload));
    }
  },
  actions: {}
});
export default store;
